import path from 'node:path'
import vue from '@vitejs/plugin-vue'
import unoCSS from 'unocss/vite'
import { defineConfig } from 'vite'
import { createHtmlPlugin } from 'vite-plugin-html'
import { watchAndRun } from 'vite-plugin-watch-and-run'

export default defineConfig({
  plugins: [
    vue(),
    unoCSS(),
    createHtmlPlugin({
      template: 'content.html',
    }),
    watchAndRun([
      {
        watch: path.resolve('src/**/*.*'),
        run: 'pnpm build:dev',
        delay: 500,
      },
    ]),
  ],
  build: {
    lib: {
      name: '__lb__plugin',
      entry: [path.resolve(__dirname, 'src/content/main.ts')],
      formats: ['iife'],
      fileName: () => {
        return 'content.js'
      },
    },
    rollupOptions: {
      output: {
        assetFileNames: () => {
          return 'content.css'
        },
      },
    },
    outDir: 'dist/content',
    chunkSizeWarningLimit: 2000,
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src'),
    },
  },
  server: {
    host: '0.0.0.0',
    port: 3020,
  },
  define: {
    'process.env.NODE_ENV': null,
  },
})
